#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def my_calc_IV(Xvar, Yvar): 
    '''
    Xvar:需要经过面元切分
        9597         [600000.0, inf)
        9598         [600000.0, inf)
        9599    [400000.0, 500000.0)
        9600         [600000.0, inf)
    Yvar:
        9597         0
        9598         0
        9599         1
        9600         0
    '''
    N_0 = np.sum(Yvar==0)
    N_1 = np.sum(Yvar==1)
    df_temp = pd.concat([Xvar, Yvar], axis=1)
    df_temp.columns = ['x','y']
    try:
        df_temp['x'] = df_temp['x'].cat.add_categories(['nan'])
    except Exception:
        pass
    df_temp['x'] = df_temp['x'].fillna('nan')
    df_temp.dropna(axis=0, how='any', inplace=True)
    
    li1 = df_temp.x.value_counts().index
    li2 = df_temp.y.value_counts().index
    df_temp['new_col'] = df_temp['x'].apply(lambda x:str(x)) + df_temp['y'].apply(lambda x:str(x))
    df_temp = df_temp['new_col'].value_counts().sort_index()

    val_li = [str(x)+str(y) for x in li1 for y in li2]
    for i in val_li:
        try:
            df_temp[i]
        except KeyError:
            df_temp[i] = 1
    df_temp = df_temp.sort_index()
    iv = 0
    for i in range(int(len(df_temp)/2)):
        p0 = df_temp[i*2]/N_0
        p1 = df_temp[i*2+1]/N_1
        if df_temp[i*2] == 1 and df_temp[i*2+1] ==1:
            pass
        else:
            iv += (p0-p1) * np.log(p0/p1)
    return  iv

if __name__ == '__main__':
    pass
#   需要先cut切分
#    my_calc_IV(pd.cut(df_xy[i],10) ,df_xy['maxdpd'])
